package com.lehezn.opensource.appkit.ControlModule;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.lehezn.opensource.appkit.R;
import java.util.List;

/**
 * Created by es-ll on 17/10/8.
 */

public class ListviewAdapter  extends BaseAdapter{
    List<MeterInfo> meterInfoList;
    Context context;

    public ListviewAdapter(List<MeterInfo> meterInfoList,Context context){
       this.meterInfoList=meterInfoList;
        this.context=context;
    }

    @Override
    public int getCount() {
        return meterInfoList.size();
    }

    @Override
    public Object getItem(int i) {
        return meterInfoList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

//        Context context=view.getContext();
        Holder holder;

        if (view == null) {
            view = LayoutInflater.from(context).inflate(
                    R.layout.activity_a_main_list_item, null);
            holder = new Holder();
            holder.icon= (ImageView) view.findViewById(R.id.iv_item_icon);
            holder.tv_name= (TextView) view.findViewById(R.id.tv_item_name);
            holder.tv_middle= (TextView) view.findViewById(R.id.tv_item_middle);
            holder.tv_right= (TextView) view.findViewById(R.id.tv_item_right);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        final MeterInfo meterInfo = meterInfoList.get(i);
        holder.icon.setBackgroundResource(meterInfo.getIcon());
        holder.tv_name.setText(meterInfo.name);
        String unit="度";
        if (meterInfo.type==Util.TYPE_WATER){
            unit="吨";
        }
        holder.tv_middle.setText(meterInfo.getCurMonthEnery()+unit);
        holder.tv_right.setText(meterInfo.getCurDayEnery()+unit);

        return view;
    }
    class Holder{
        public ImageView icon;
        public TextView tv_name,tv_middle,tv_right;

    }
}
